< previous page page_175 next page >

Page 175
Solution 10
Environmentally Speaking
What exactly does the GetEnvironmentStrings function return? According to the documentation, it returns a pointer to a block of memory that contains environment variables separated by NULL characters. The final variable is terminated by two NULL characters. Let's say you had two environment variables as follows:
Temp=C:\TEMP
OS=NT
These would appear in memory as shown below:
0175-01.gif
From Tutorial 6, ''C++ Variables Meet Visual Basic," you know that a Visual Basic string is actually an OLE string or BSTR. A BSTR is a string that is managed (allocated and deallocated) by the OLE subsystem. A BSTR pointer points to the start of the string data. The 32 bits before that location contain the length of the string. Also, keep in mind that BSTR strings used by Visual Basic usually contain Unicode data.
How do we know that the GetEnvironmentStrings function does not return a BSTR?
The buffer illustrated above clearly does not include any length data for the string.
The GetEnvironmentStrings function is part of kernel32 (the core operating system DLL), not the OLE subsystem. Only the OLE subsystem knows how to handle BSTR strings.
The documentation requires that you call the FreeEnvironmentStrings function to free the buffer. A special function would not be required for a BSTR, which could be freed by the OLE SysFreeString function.

 
< previous page page_175 next page >